﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using COFE.Core;

namespace COFE.Core
{
    /// <summary>
    /// Extends IFileInfo.
    /// </summary>
    public static class IFileInfoExAExtension
    {
        /// <summary>
        /// reates a StreamWriter that appends UTF-8 encoded text to current file.
        /// </summary>
        /// <returns></returns>
        public static StreamWriter AppendText(this IFileInfo file)
        {
            return new StreamWriter(file.Open(FileMode.Append, FileAccess.ReadWrite));
        }

        /// <summary>
        /// Creates or overwrites current file.
        /// </summary>
        /// <returns></returns>
        public static Stream Create(this IFileInfo file)
        {
            return file.Open(FileMode.Create, FileAccess.ReadWrite, FileShare.None);
        }

        /// <summary>
        /// Open a FileStream for current file.
        /// </summary>
        /// <param name="mode"></param>
        /// <param name="access"></param>
        /// <param name="share"></param>
        /// <returns></returns>
        public static Stream Open(this IFileInfo file, FileMode mode, FileAccess access, FileShare share)
        {
            var f = (file as IInternalFileInfoExA);

            return f.StreamProvider.OpenStream(f, mode, access, share);
        }

        /// <summary>
        /// Open a FileStream for current file.
        /// </summary>
        /// <param name="mode"></param>
        /// <param name="access"></param>
        /// <returns></returns>
        public static Stream Open(this IFileInfo file, FileMode mode, FileAccess access)
        {
            FileShare fShare = (access == FileAccess.Read) ? FileShare.Read : FileShare.None;            
            return file.Open(mode, access, fShare);
        }

        /// <summary>
        /// Open a FileStream for current file.
        /// </summary>
        /// <param name="mode"></param>
        /// <returns></returns>
        public static Stream Open(this IFileInfo file, FileMode mode)
        {
            FileAccess fAccess = (mode == FileMode.Open) ? FileAccess.Read : FileAccess.ReadWrite;                        
            return file.Open(mode, fAccess);
        }

        /// <summary>
        /// Open a FileStream for current file.
        /// </summary>
        /// <returns></returns>
        public static Stream Open(this IFileInfo file)
        {
            return file.Open(FileMode.Open);
        }

        /// <summary>
        /// Open current file for reading.
        /// </summary>
        /// <returns></returns>
        public static Stream OpenRead(this IFileInfo file)
        {
            return file.Open(FileMode.Open, FileAccess.Read, FileShare.Read);
        }

        /// <summary>
        /// Open current file for writing.
        /// </summary>
        /// <returns></returns>
        public static Stream OpenWrite(this IFileInfo file)
        {
            return file.Open(FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
        }

        /// <summary>
        /// Opens this file as an existing UTF-8 encoded text file for reading.
        /// </summary>
        /// <returns></returns>
        public static StreamReader OpenText(this IFileInfo file)
        {
            return new StreamReader(file.OpenRead());
        }

       

        
    }
}
